package com.example.database;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.ChooseCurriculum;

import java.util.ArrayList;
import java.util.List;

public class MyCourseMessage extends SQLiteOpenHelper {
    private static  final  String DB_NAME = "MyCourse" ;
    private static final int DB_Version = 1 ;
    private Context context;

    public MyCourseMessage(@Nullable Context context) {
        super(context, DB_NAME, null, DB_Version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE choose (" +
                "chooseId TEXT NOT NULL PRIMARY KEY ," +
                "studentId TEXT NOT NULL ,"+
                "curriculumId Text NOT NULL )";
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS choose");
        onCreate(db);
    }

    public void addChoose(String studentId , String curriculumId){
        SQLiteDatabase db = this.getWritableDatabase();
        String chooseId = studentId + curriculumId ;

        ContentValues values = new ContentValues();
        values.put("chooseId" , chooseId);
        values.put("studentId" , studentId);
        values.put("curriculumId" , curriculumId);

        long newRowId = db.insert("choose" , null ,values);

    }
    public boolean isCourseChosen(String studentId, String curriculumId) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = { "chooseId" };
        String selection = "studentId = ? AND curriculumId = ?";
        String[] selectionArgs = { studentId, curriculumId };
        Cursor cursor = db.query("choose", columns, selection, selectionArgs, null, null, null);

        if (cursor.getCount() > 0) {
            cursor.close();
            return true; // 课程已经被选择
        } else {
            cursor.close();
            return false; // 课程未被选择
        }
    }

    // 删除选课记录的方法
    public boolean deleteChoose(String studentId, String curriculumId) {
        SQLiteDatabase db = this.getWritableDatabase();

        // 构建删除条件
        String whereClause = "studentId = ? AND curriculumId = ?";
        String[] whereArgs = { studentId, curriculumId };

        // 执行删除操作
        int rowsDeleted = db.delete("choose", whereClause, whereArgs);

        // 返回删除的行数，如果大于0，则表示删除成功
        return rowsDeleted > 0;
    }

    // 获取特定studentId选择的所有curriculumId
    public List<String> getChosenCurriculumIds(String studentId) {
        List<String> curriculumIds = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = { "curriculumId" };
        String selection = "studentId = ?";
        String[] selectionArgs = { studentId };
        Cursor cursor = null;
        try {
            cursor = db.query("choose", columns, selection, selectionArgs, null, null, null);
            while (cursor.moveToNext()) {
                String curriculumId = cursor.getString(0); // 索引0对应curriculumId
                curriculumIds.add(curriculumId);
            }
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
        return curriculumIds;
    }
}
